package com.xidian.zhiancloud.service.chemicalsManagement.chemicalManager.wastePart;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xidian.zhiancloud.constant.DangerousManagementConstant;
import com.xidian.zhiancloud.dao.WasteStorageMapper;
import com.xidian.zhiancloud.dao.syset.SysUserMapper;
import com.xidian.zhiancloud.enums.GeneralResponseEnums;
import com.xidian.zhiancloud.exception.CustomizeException;
import com.xidian.zhiancloud.model.WasteInfo;
import com.xidian.zhiancloud.model.WasteStorage;
import com.xidian.zhiancloud.model.WasteType;
import com.xidian.zhiancloud.model.syset.SysUser;
import com.xidian.zhiancloud.qo.chemicalsManagement.WasteStorageQO;
import com.xidian.zhiancloud.service.chemicalsManagement.chemicalManager.CodeService;
import com.xidian.zhiancloud.util.zyf.ExcelUtils;
import com.xidian.zhiancloud.vo.chemicalsManagement.WasteStorageVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @Author: Egoist
 * @Date: 2020/11/10 17:00
 */

@Service
public class WasteStorageService extends ServiceImpl<WasteStorageMapper, WasteStorage> implements IService<WasteStorage> {

    @Autowired
    private WasteStorageMapper wasteStorageMapper;
    @Autowired
    private SysUserMapper sysUserMapper;
    @Autowired
    private WasteInfoService wasteInfoService;
    @Autowired
    private WasteTypeService wasteTypeService;
    @Autowired
    private CodeService codeService;

    public IPage<WasteStorageVO> getListByQueryObject(WasteStorageQO wasteStorageQO) {
        Page<WasteStorageVO> page = new Page<>(wasteStorageQO.getCurrent(), wasteStorageQO.getSize());
        OrderItem orderItem = new OrderItem();
        orderItem.setColumn("storage_time").setAsc(false);
        page.getOrders().add(orderItem);
        IPage<WasteStorageVO> iPage = wasteStorageMapper.getListByQO(page, wasteStorageQO);
        List<WasteStorageVO> records = iPage.getRecords();
        List<WasteStorageVO> ans = new ArrayList<>();
        records.forEach(t -> {
            t.setStrQuantity(t.getQuantity().toString() + DangerousManagementConstant.QuantityUnit.get(t.getUnit()));
            ans.add(t);
        });
        iPage.setRecords(ans);
        return iPage;
    }

    public IPage<WasteStorageVO> getAccountListByQueryObject(WasteStorageQO wasteStorageQO) {
        Page<WasteStorageVO> page = new Page<>(wasteStorageQO.getCurrent(), wasteStorageQO.getSize());
        OrderItem orderItem = new OrderItem();
        orderItem.setColumn("storage_time").setAsc(false);
        page.getOrders().add(orderItem);
        IPage<WasteStorageVO> iPage = wasteStorageMapper.getAccountListByQO(page, wasteStorageQO);
        List<WasteStorageVO> records = iPage.getRecords();
        List<WasteStorageVO> ans = new ArrayList<>();
        records.forEach(t -> {
            t.setStrQuantity(t.getQuantity().toString() + DangerousManagementConstant.QuantityUnit.get(t.getUnit()));
            ans.add(t);
        });
        iPage.setRecords(ans);
        return iPage;
    }

    public WasteStorageVO getVOById(Long id) {
        WasteStorage wasteStorage = getById(id);
        if (wasteStorage == null) {
            throw new CustomizeException(GeneralResponseEnums.GET_FAILED);
        }
        WasteStorageVO wasteStorageVO = new WasteStorageVO();
        BeanUtils.copyProperties(wasteStorage, wasteStorageVO);
        Long wasteInfoId = wasteStorage.getWasteId();
        Long contactId = wasteStorage.getContactId();
        Long typeId = wasteStorage.getTypeId();


        WasteType wasteType = wasteTypeService.getById(typeId);
        SysUser sysUser = sysUserMapper.selectById(contactId);
        WasteInfo wasteInfo = wasteInfoService.getById(wasteInfoId);

        wasteStorageVO.setContactName(sysUser == null ? "该员工已删除" : sysUser.getName());
        wasteStorageVO.setWasteName(wasteInfo == null ? "该废弃物信息已删除" : wasteInfo.getWasteName());
        wasteStorageVO.setWasteType(wasteType == null ? "该类型已删除" : wasteType.getWasteType());

        return wasteStorageVO;
    }

    public Boolean submitById(Long id) {
        WasteStorage wasteStorage = getById(id);
        WasteInfo info = wasteInfoService.getById(wasteStorage.getWasteId());
        info.setRealQuantity(info.getRealQuantity().add(wasteStorage.getQuantity()));
        info.setState( info.getMaxQuantity().compareTo(info.getRealQuantity()) == 1 ? 0 : 1);
        wasteInfoService.updateById(info);
        String code = codeService.getWasteStorageCode(wasteStorage.getCompanyId());
        wasteStorage.setStorageCode(code);
        wasteStorage.setState(1);
        return updateById(wasteStorage);
    }


    public Boolean export(HttpServletResponse response, Long[] ids) throws IOException {
        List<WasteStorage> list = listByIds(Arrays.asList(ids));
        List<WasteStorageVO> ans = new ArrayList<>();
        list.forEach(t -> {
            WasteStorageVO vo = new WasteStorageVO();
            BeanUtils.copyProperties(t, vo);
            SysUser contactUser = sysUserMapper.selectById(vo.getContactId());
            SysUser storageUser = sysUserMapper.selectById(vo.getStoragePersonId());
            WasteInfo info = wasteInfoService.getById(vo.getWasteId());
            WasteType type = wasteTypeService.getById(info.getTypeId());

            vo.setContactName(contactUser == null ? "该用户已删除" : contactUser.getName());
            vo.setStoragePersonName(storageUser == null ? "该用户已删除" : storageUser.getName());
            vo.setWasteName(info.getWasteName());
            vo.setWasteType(type.getWasteType());
            vo.setStrQuantity(vo.getQuantity().toString() + DangerousManagementConstant.QuantityUnit.get(info.getUnit()));

            ans.add(vo);
        });

        ExcelUtils.exportExcel(ans,"废弃物入库台账","废弃物入库",WasteStorageVO.class,"废弃物入库台账",response);
        return true;
    }


}
